package com.csxy.jqr.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csxy.jqr.R;
import com.csxy.jqr.entity.EduTeacher;
import com.csxy.jqr.entity.vo.TeacherQuery;
import com.csxy.jqr.service.EduTeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "edu-teacher", description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;
    // ————————————————————————逻辑——————————————————
//    @Operation(summary = "查询所有讲师")
//    @GetMapping
//    public List<EduTeacher> getAllTeacher() {
//        List<EduTeacher> list = eduTeacherService.list();
//        return list;
//    }

    // 逻辑删除，数据库中字段is_deleted = 1，而不是直接删除掉数据
//    @Operation(summary = "逻辑删除讲师")
//    @DeleteMapping("/deleteById/{id}")
//    public boolean deleteById(@PathVariable("id") String id) {
//        return eduTeacherService.removeById(id);
//    }

    //————————————————————————————逻辑，统一格式--R——————————————————————
    @Operation(summary = "查询所有讲师")
    @GetMapping("findAll")
    public R getAllTeacher() {
        List<EduTeacher> list = eduTeacherService.list();
        return R.ok().data("items", list);
    }

    @Operation(summary = "逻辑删除讲师")
    @DeleteMapping("/deleteById/{id}")
    public R deleteById(@PathVariable("id") String id) {
        boolean result = eduTeacherService.removeById(id);
        if (!result){
            return R.error();
        }
        return R.ok();
    }
    // 分页查询
    @Operation(summary = "分页查询讲师")
    @GetMapping("/pageList/{page}/{limit}")
    public R pageListTeacher(@PathVariable("page") Long page,
                             @PathVariable("limit") Long limit) {
        Page<EduTeacher> pageTeacher = eduTeacherService.pageListTeacher(page, limit);
        return R.ok().data("total", pageTeacher.getTotal()).data("rows", pageTeacher.getRecords());
    }
    // 新增讲师
    @Operation(summary = "新增讲师",description = "新增讲师")
    @PostMapping("/save")
    public R save(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.save(eduTeacher);
        if(flag){
            return R.ok();
        }
        return R.error();
    }
    // 根据id查询，用于信息回显
    @Operation(summary = "根据id查询讲师",description = "根据id查询讲师")
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable String id) {
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("item", teacher);
    }
    // 修改讲师
    @Operation(summary = "修改讲师",description = "修改讲师")
    @PostMapping("/updateById")
    public R updateById(@RequestBody EduTeacher teacher) {
        boolean flag = eduTeacherService.updateById(teacher);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
    // 多条件查询讲师带分页
    @Operation(summary = "多条件查询讲师带分页",description = "多条件查询讲师带分页")
    @PostMapping("/pageTeacherCondition/{page}/{limit}")
    public R pageListCondition(@Parameter(name = "page",description = "当前页码",required = true)
                               @PathVariable Long page,
                               @Parameter(name = "limit",description = "每页记录数",required = true)
                               @PathVariable Long limit,
                               @RequestBody(required = false)TeacherQuery teacherQuery){//通过封装TeacherQuery对象，实现多条件查询
        // 创建分页page对象
        Page<EduTeacher> pageParam = new Page<>(page, limit);
        // 调用service方法实现多条件查询分页
        eduTeacherService.pageQuery(pageParam, teacherQuery);
        // 获取查询到的数据
        List<EduTeacher> records = pageParam.getRecords();
        // 获取总记录数
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }
}
